package cn.ekuma.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.jtimepiece.TimeUtil;

public abstract class AbstractTimeGroupCalc<T,K> {
     Map<Date,T> groupObjs;
     
     public static int TIMERCYCLETYPE_Hour=0;
     public static int TIMERCYCLETYPE_Today=1;
     public static int TIMERCYCLETYPE_Week=2;
     public static int TIMERCYCLETYPE_Month=3;
     
     public List<T> calc(List<K> inObjs,int groupDateType){
    	 groupObjs=new TreeMap<Date,T>();
    	 Date key=null;
    	 T temp;
    	 for(K obj:inObjs){
    		key=trim(getKey(obj),groupDateType);
    		if(!groupObjs.containsKey(key)){
    			groupObjs.put(key, createNew(obj,key));
    		}else{
    			op(groupObjs.get(key),obj);
    		}
    	 }	 
    	 return new ArrayList(groupObjs.values());
     } 
     
     public abstract T createNew(K inObj,Date date);
     
     public abstract Date getKey(K inObj);
     
     public abstract void op(T total,K inObj);
     
     private Date trim(Date inDate,int type){
    	 Date ret=inDate;
    	 switch(type){
    	 case 0:
    		 ret=TimeUtil.GetDateNoTimeOfHour(inDate);
    		 break;
    	 case 1:
    		 ret=TimeUtil.GetDateNoTime(inDate);
    		 break;
    	 case 2:
    		 ret=TimeUtil.getFirstDateForWeekOfDate(inDate);
    		 break;
    	 case 3:
    		 ret=TimeUtil.GetDateNoTimeOfMouth(inDate);
    	 }
    	 return ret;
     }
}
